package cn.lunkr.sdkdemo.ui.table.activity;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.slidingpanelayout.widget.SlidingPaneLayout;

import cn.lunkr.base.config.Actions;
import cn.lunkr.base.entity.mail.SimpleMail;
import cn.lunkr.base.ui.BasicActivity;
import cn.lunkr.mailui.ui.MailsFragment;
import cn.lunkr.mailui.ui.listener.ReadMailBarListener;
import cn.lunkr.mailui.ui.mail.ReadMailBarFragment;
import cn.lunkr.mailui.ui.search.SearchMailsBarFragment;
import cn.lunkr.sdkdemo.R;
import cn.lunkr.sdkdemo.databinding.ActivityTableSearchBinding;

public class TableSearchActivity extends BasicActivity
        implements SearchMailsBarFragment.SearchMailsBarOnItemClickListener, ReadMailBarListener {
    private ActivityTableSearchBinding mViewBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewBinding = ActivityTableSearchBinding.inflate(getLayoutInflater());
        setContentView(mViewBinding.getRoot());
        initCallBack();
    }

    @Override
    public boolean onSearchMailBarBack() {
        return false;
    }

    @Override
    public boolean onMailItemClick(SimpleMail simpleMail, int position) {
        Bundle bundle = new Bundle();// todo 读信数据来源未统一 & readMode模式未传入
        bundle.putParcelable(MailsFragment.ITEM_BUNDLE_MAIL, simpleMail);
        Fragment fragment = getSupportFragmentManager().findFragmentByTag("read_frag");
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (fragment == null) {
            ft.replace(R.id.mail_container, ReadMailBarFragment.class, bundle, "read_frag");
        } else {
            Intent intent = new Intent(Actions.ACTION_READ_MAIL(this));
            intent.putExtras(bundle);
            LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
        }
        if (mViewBinding.slidingPaneLayout.isOpen()) {
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        }
        ft.commit();
        mViewBinding.slidingPaneLayout.open();
        return true;
    }


    @Override
    public void slideMailView(boolean needExpand) {
        if (mViewBinding.slidingPaneLayout.isSlideable()) {
            return;
        }
        View detailView = mViewBinding.slidingPaneLayout.getChildAt(1);
        if (detailView == null) return;
        if (needExpand) {
            ObjectAnimator a1 = ObjectAnimator.ofFloat(mViewBinding.searchContainer, "X", 1f, 0f);
            ObjectAnimator a2 = ObjectAnimator.ofFloat(mViewBinding.searchContainer, "Y", 1f, 0f);
            AnimatorSet set = new AnimatorSet();
            set.play(a1).with(a2);
            set.setDuration(500).start();
            mViewBinding.mailContainer.setVisibility(View.GONE);
        } else {
            mViewBinding.mailContainer.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void preOrNext(SimpleMail mail) {
        // 查询结果不支持上下封阅读
    }

    @Override
    public void delete(String mailId) {

    }

    @Override
    public void close() {
        Fragment readMailFrag = getSupportFragmentManager().findFragmentById(R.id.mail_container);
        if (readMailFrag == null) return;
        getSupportFragmentManager().beginTransaction().remove(readMailFrag).commit();
    }

    private void initCallBack() {
        getOnBackPressedDispatcher().addCallback(
                this,
                new EmailOnBackPressedCallback(mViewBinding.slidingPaneLayout));
    }

    /**
     * 响应 单侧情况返回到列表frag
     */
    private static class EmailOnBackPressedCallback extends OnBackPressedCallback
            implements SlidingPaneLayout.PanelSlideListener {

        private final SlidingPaneLayout mSlidingPaneLayout;


        EmailOnBackPressedCallback(@NonNull SlidingPaneLayout slidingPaneLayout) {
            super(slidingPaneLayout.isSlideable() && slidingPaneLayout.isOpen());
            mSlidingPaneLayout = slidingPaneLayout;
            slidingPaneLayout.addPanelSlideListener(this);
        }

        @Override
        public void handleOnBackPressed() {
            mSlidingPaneLayout.closePane();
        }

        @Override
        public void onPanelSlide(@NonNull View panel, float slideOffset) {
        }

        @Override
        public void onPanelOpened(@NonNull View panel) {
            setEnabled(true);
        }

        @Override
        public void onPanelClosed(@NonNull View panel) {
            setEnabled(false);
        }
    }

}